package org.ki.gra.aktorzy;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Actor;

/**
 * User: kieronsk
 * Date: 10/9/12
 * Time: 5:39 PM
 */
public class AnimowanyAktor extends Actor {

    private com.badlogic.gdx.graphics.g2d.Animation animacja;
    private float czas = 0f;
    private final TextureRegion[] klatki;
    private final float proporcje;

    public float getProporcje() {
        return proporcje;
    }

    public AnimowanyAktor(String fileName,int kol, int wie, float czas) {
        super();

        Texture tekstura = new Texture(Gdx.files.internal(fileName));
        int szer = tekstura.getWidth() / kol;
        int wys = tekstura.getHeight() / wie;

        proporcje = szer/(float) wys;

        TextureRegion[][] tmp = TextureRegion.split(tekstura, szer, wys);

        klatki = new TextureRegion[kol*wie];

        int index = 0;
        for (int i=0; i<wie; i++)
            for (int j=0; j<kol; j++)
                klatki[index++] = tmp[i][j];

        animacja = new Animation(czas/(float)(kol*wie), klatki);
    }

    @Override
    public void draw(SpriteBatch batch, float parentAlpha) {
        czas += Gdx.graphics.getDeltaTime();
        Color c = this.color;
        c.a *= parentAlpha;
        batch.setColor(c);
        batch.draw(animacja.getKeyFrame(czas,true),x,y,originX,originY,width,height,scaleX,scaleY,rotation);
    }

    @Override
    public Actor hit(float x, float y) {
        if (x>0 && x<this.width && y>0 && y<this.height)
            return this;
        return null;
    }

    @Override
    public boolean touchDown(float x, float y, int pointer) {
        System.out.println("x = [" + x + "], y = [" + y + "], pointer = [" + pointer + "]");
        System.out.println("name = " + name);
        return super.touchDown(x, y, pointer);
    }

    public void setSpeed(float speed) {
        animacja = new Animation(speed, klatki);
    }

}
